﻿<%@ Page Title="Permintaan Barang" Language="C#" MasterPageFile="~/Lab.master" AutoEventWireup="true" CodeFile="PermintaanBarangUser.aspx.cs" Inherits="PermintaanBarangUser" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="BodyContent" Runat="Server">
    <div class="col-md-12 col-sm-12 col-xs-12">
        <div class="x_panel">
            <div class="x_title">
                <h2>Daftar Permintaan Barang</h2>
                <ul class="nav navbar-right panel_toolbox">
                    <li><a class="collapse-link"><i class="fa fa-chevron-down"></i></a>
                    </li>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
                        <ul class="dropdown-menu" role="menu">
                            <li><a href="#">Settings 1</a>
                            </li>
                            <li><a href="#">Settings 2</a>
                            </li>
                        </ul>
                    </li>
                    <li><a class="close-link"><i class="fa fa-close"></i></a>
                    </li>
                </ul>
                <div class="clearfix"></div>
            </div>
            <div class="x_content" style="display: none">
                <form id="formDisplay" class="form-horizontal form-label-left" method="post">
                    <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="barang">TAHUN ANGGARAN</label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                            <select class="select2_tahun form-control" tabindex="-1" name="displayTahun" id="displayTahun">
                                <asp:Repeater runat="server" ID="lstTahunAnggaran2">
                                    <HeaderTemplate>
                                        <option></option>
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <option value="<%# DataBinder.Eval(Container.DataItem, "TAHUN") %>"><%# DataBinder.Eval(Container.DataItem, "TAHUN") %></option>
                                    </ItemTemplate>
                                </asp:Repeater>
                             </select>
                        </div>
                    </div>
                </form>
                
                <table id="datatable-responsive" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
                    <thead>
                        <tr class="headings">
                            <th class="column-title">Nama Barang</th>
                            <th class="column-title">Jumlah Permintaan</th>
                            <th class="column-title">Keterangan</th>
                        </tr>
                    </thead>
                    <tbody>
                        
                    </tbody>
                </table>
            </div>
        </div>

        <div class="x_panel">
            <div class="x_title">
                <h2>Input Permintaan Barang</h2>
                <ul class="nav navbar-right panel_toolbox">
                    <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
                    </li>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
                        <ul class="dropdown-menu" role="menu">
                            <li><a href="#">Settings 1</a>
                            </li>
                            <li><a href="#">Settings 2</a>
                            </li>
                        </ul>
                    </li>
                    <li><a class="close-link"><i class="fa fa-close"></i></a>
                    </li>
                </ul>
                <div class="clearfix"></div>
            </div>
            <div class="x_content">
                <form id="myForm" class="form-horizontal form-label-left" method="post">
                    <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="barang">TAHUN ANGGARAN<span class="required">*</span></label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                            <select class="select2_tahun form-control" tabindex="-1" name="tahun" id="tahun" required="required">
                                <asp:Repeater runat="server" ID="lstTahunAnggaran">
                                    <HeaderTemplate>
                                        <option></option>
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <option value="<%# DataBinder.Eval(Container.DataItem, "TAHUN") %>"><%# DataBinder.Eval(Container.DataItem, "TAHUN") %></option>
                                    </ItemTemplate>
                                </asp:Repeater>
                              </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="barang">Barang<span class="required">*</span></label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                            <select class="select2_barang form-control" tabindex="-1" name="barang" id="barang" required="required">
                                <asp:Repeater runat="server" ID="lstBarang">
                                    <HeaderTemplate>
                                        <option></option>
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <option value="<%# DataBinder.Eval(Container.DataItem, "ID") %>"><%# DataBinder.Eval(Container.DataItem, "NAMA") %></option>
                                    </ItemTemplate>
                                </asp:Repeater>
                              </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="jumlah">Jumlah <span id="txtUnit"></span><span class="required">*</span></label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                            <input id="jumlah" class="form-control col-md-7 col-xs-12" type="number" step="0.00001" name="jumlah" required >
                        </div>
                    </div>
                    
                    <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12">Keterangan</label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                            <textarea id="keterangan" class="form-control col-md-7 col-xs-12" rows="5" name="keterangan"></textarea>
                        </div>
                    </div>

                    <input style="visibility:collapse" id="user" class="form-control col-md-7 col-xs-12" type="text" name="user" required value="<%=Session["idUser"]%>" />

                    <div class="ln_solid"></div>
                    <div class="form-group">
                        <div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
                            <a href="LabPermintaanBarang.aspx" class="btn btn-danger">Batal</a>
                            <button id="myButton" class="btn btn-success">Kirim Permintaan</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="BodyFooterContainer" Runat="Server">
    <script type="text/javascript">  
        $('#displayTahun').change(function () {
            refreshTablePermintaan();
        });

        function refreshTablePermintaan() {
            if ($("#displayTahun").val() == '') {
                return;
            }
            var tahun = $("#displayTahun").val();
            var user = $("#user").val();
            var jsonData = JSON.stringify({ tahun: tahun, user: user });
            $.ajax({
                type: "POST",
                url: "PermintaanBarangUser.aspx/ListPermintaan",
                data: jsonData,
                contentType: "application/json; charset=utf-8",
                dataType: "json", // dataType is json format
                success: OnSuccess,
                error: OnErrorCall
            });

            function OnSuccess(response) {
                if (response.d.length > 0) {
                    $('#datatable-responsive tbody').empty();
                    $('#datatable-responsive tr').not(':first').remove();
                    var html = '';
                    for (var i = 0; i < response.d.length; i++)
                        html += '<tr><td>' + response.d[i].NamaBarang + '</td><td>' + response.d[i].Jumlah + ' ' + response.d[i].Satuan + '</td><td>' + response.d[i].Keterangan + '</td></tr>';
                    $('#datatable-responsive tbody').append(html);
                }
                else {
                    //$('#datatable-responsive tr').not(':first').remove();
                    $('#datatable-responsive tbody').empty();
                    new PNotify({
                        title: 'Daftar Permintaan',
                        text: 'Data permintaan untuk tahun anggaran ' + tahun + ' belum/tidak ada.',
                        type: 'warning',
                        styling: 'bootstrap3'
                    });
                }
            }
            function OnErrorCall(response) {
                new PNotify({
                    title: 'Gagal mengambil data permintaan barang',
                    text: response.statusText + ' (' + response.status + ') : ' + response.responseJSON.Message,
                    type: 'error',
                    styling: 'bootstrap3'
                });
            }
        }
        
        $('#barang').change(function () {
            if ($("#barang").val() == '') {
                return;
            }

            var jsonData = JSON.stringify({ barang: $("#barang").val() });
            $.ajax({
                type: "POST",
                url: "GudangBarangMasuk.aspx/GetUnit",
                data: jsonData,
                contentType: "application/json; charset=utf-8",
                dataType: "json", // dataType is json format
                success: OnSuccess,
                error: OnErrorCall
            });

            function OnSuccess(response) {
                if (response.d.Status == true && response.d.Value != '') {
                    $('#txtUnit').text('(' + response.d.Value + ') ');
                }
                else {
                    new PNotify({
                        title: 'Error mengambil unit barang',
                        text: response.d.ErrorMessage,
                        type: 'error',
                        styling: 'bootstrap3'
                    });
                }
            }
            function OnErrorCall(response) {
                new PNotify({
                    title: 'Gagal mengambil unit barang',
                    text: response.statusText + ' (' + response.status + ') : ' + response.responseJSON.Message,
                    type: 'error',
                    styling: 'bootstrap3'
                });
            }
        });

        //*
        $("#myButton").on("click", function (e) {

            var $myForm = $('#myForm')
            if (!$myForm[0].checkValidity()) {
                $myForm.find(':submit').click();
                return;
            }

            e.preventDefault();
            var aData = [];
            var tahun = $("#tahun").val();
            var barang = $("#barang").val();
            var jumlah = $("#jumlah").val();
            //var lab = $("#lab").val();
            var keterangan = $("#keterangan").val();
            var user = $("#user").val();
            var jsonData = JSON.stringify({ tahun: tahun, barang: barang, jumlah: jumlah, keterangan: keterangan, user: user });
            $.ajax({
                type: "POST",  
                url: "PermintaanBarangUser.aspx/Permintaan",
                data: jsonData,
                contentType: "application/json; charset=utf-8",
                dataType: "json", // dataType is json format
                success: OnSuccess,
                error: OnErrorCall
            });

            function OnSuccess(response) {
                if (response.d.Status == true) {
                    new PNotify({
                        title: 'Permintaan Barang Berhasil',
                        text: 'Data sudah berhasil ditambahkan',
                        type: 'success',
                        styling: 'bootstrap3'
                    });
                    $("#tahun").val('').trigger('change');
                    $("#barang").val('').trigger('change');
                    //$("#labAsal").val('').trigger('change');
                    if ($("#lab option[value=<%=Session["idLab"]%>]").length > 0) {
                        $("#lab option[value=<%=Session["idLab"]%>]").attr('selected', 'selected');
                        $("#lab option[value=<%=Session["idLab"]%>]").change();
                        $('#lab').prop('disabled', 'disabled');
                    }
                    else
                    {
                        $('#lab').prop('disabled', false);
                    }
                    $("#labTujuan").val('').trigger('change');
                    $('#myForm').trigger("reset");
                    refreshTablePermintaan();
                    // generate tanda terima
                }
                else
                {
                    new PNotify({
                        title: 'Permintaan Barang Gagal',
                        text: response.d.ErrorMessage,
                        type: 'error',
                        styling: 'bootstrap3'
                    });
                }
            }
            function OnErrorCall(response) {
                new PNotify({
                    title: 'Gagal Mengirim Data',
                    text: response.statusText + ' (' + response.status + ') : ' + response.responseJSON.Message,
                    type: 'error',
                    styling: 'bootstrap3'
                });
            }
        });
        //*
    </script>  
</asp:Content>